﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.GraphicsInterface;
using System.Collections.Generic;
using System.Linq;
using System;

namespace UseEntityJig
{
    public class DrawBundleWithNodesJig : DrawJig
    {
        private Point3d startPoint;
        private Point3d endPoint;
        private Line _line;
        private DBText _lengthText; // 用于显示动态长度
        private double nodeRadius;

        public DrawBundleWithNodesJig(Line line, Point3d startPoint, double nodeRadius)
        {
            this.startPoint = startPoint;
            this.endPoint = startPoint;
            this.nodeRadius = nodeRadius;
            // 初始化线条实体
            this._line = line;

            // 初始化文本实体
            _lengthText = new DBText
            {
                Height = 2.5,
                Position = endPoint,
                TextString = "0",
                Layer = "0",
                Color = Color.FromColorIndex(ColorMethod.ByAci, 7)// 白色
            };

        }

        // 公有属性用于获取终点
        public Point3d EndPoint => endPoint;

        /// <summary>
        /// 重载函数 拖动时自动执行
        /// </summary>
        /// <returns></returns>
        protected override bool WorldDraw(WorldDraw draw)
        {
            // 更新直线的起点和终点
            _line.StartPoint = startPoint;
            _line.EndPoint = endPoint;// 拖动时自动更新

            // 绘制实时更新的直线
            draw.Geometry.Draw(_line);

            // 绘制实时更新的长度文本
            draw.Geometry.Draw(_lengthText);

            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions pointOptions = new JigPromptPointOptions("\n请选择结尾点: ");
            pointOptions.BasePoint = startPoint;
            pointOptions.UseBasePoint = true;
            pointOptions.Cursor = CursorType.Crosshair; // 设置鼠标为“+”形状

            PromptPointResult pointResult = prompts.AcquirePoint(pointOptions);
            if (pointResult.Status == PromptStatus.OK)
            {
                if (pointResult.Value.IsEqualTo(endPoint))
                    return SamplerStatus.NoChange;

                endPoint = pointResult.Value;
                UpdateEntities();
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }

        private void UpdateEntities()
        {
            // 更新线条的终点
            _line.EndPoint = endPoint;

            // 更新长度文本的内容和位置
            double length = _line.Length;
            _lengthText.TextString = $"{length:F0}";
            _lengthText.Position = endPoint.Add(new Vector3d(1, 1, 0));

            // 计算直线中点，并在其上方5个单位处显示长度文本
            Point3d textPosition = new Point3d(
                (startPoint.X + endPoint.X) / 2,
                (startPoint.Y + endPoint.Y) / 2 + 5,
                (startPoint.Z + endPoint.Z) / 2
            );

            //// 更新动态长度文本的内容和位置
            //dynamicLengthText.Location = textPosition;
            //dynamicLengthText.Contents = line.Length.ToString("F0");
        }

        /// <summary>
        /// 加入图形空间
        /// </summary>
        /// <param name="btr"></param>
        /// <param name="tr"></param>
        public void AppendEntities(BlockTableRecord btr, Transaction tr)
        {
            // 将线和文本都添加到模型空间
            btr.AppendEntity(_line);
            tr.AddNewlyCreatedDBObject(_line, true);
            // 绘制结束 动态长度不需要显示
            //btr.AppendEntity(_lengthText);
            //tr.AddNewlyCreatedDBObject(_lengthText, true);
        }
    }
}
